package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author jay
 */
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 添加套餐
     * @param setmealDto
     */
    //因为设计两张表的DML操作，所以加事务
    @Transactional
    public void saveWhitSetmealDish(SetmealDto setmealDto) {
        //套餐信息的添加
        this.save(setmealDto);

        //套餐菜品信息的添加
        //拿到setmealId
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getName,setmealDto.getName());
        Setmeal setmeal = this.getOne(setmealWrapper);
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //给setmealDishes的每个对象赋值setmealId
        for (int i = 0; i < setmealDishes.size(); i++) {
            setmealDishes.get(i).setSetmealId(setmealId);
        }
        //添加
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 修改套餐
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithSetmealDish(SetmealDto setmealDto) {
        //套餐基本信息修改
        this.updateById(setmealDto);

        //套餐菜品信息修改1. 先清理
        LambdaQueryWrapper<SetmealDish> setmealDishWrapper = new LambdaQueryWrapper<>();
        setmealDishWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(setmealDishWrapper);
        //2. 后添加
        //判断菜品是否为空
        if(setmealDto.getSetmealDishes() == null || setmealDto.getSetmealDishes().size() < 1){
            return;
        }
        //赋值setmealId
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (int i = 0; i < setmealDishes.size(); i++) {
            setmealDishes.get(i).setSetmealId(setmealDto.getId());
        }
        //添加
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 按ids删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(Long[] ids) {
        List<Long> setmealIds = new ArrayList<>(Arrays.asList(ids));//Long数组转化为List
        //查询套餐状态，确定是否可以删除
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.in(Setmeal::getId,setmealIds);
        setmealWrapper.eq(Setmeal::getStatus, 1);
        int count = this.count(setmealWrapper);
        //如果不能删除，抛出业务异常
        if(count > 0){
            throw new CustomException("套餐售卖中，不能删除");
        }
        // 删除套餐基本信息setmeal
        this.removeByIds(setmealIds);

        // 删除setmealDish信息
        LambdaQueryWrapper<SetmealDish> setmealDishqueryWrapper = new LambdaQueryWrapper<>();
        setmealDishqueryWrapper.in(SetmealDish::getSetmealId,setmealIds);
        setmealDishService.remove(setmealDishqueryWrapper);
    }
}
